package com.smarthouse.service;

import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.smarthouse.util.JwtUtil;

@Service
public class UserAccountService {
    private static final Logger logger = LoggerFactory.getLogger(UserAccountService.class);
    
    @Autowired
    private JwtUtil jwtUtil;
    
    @Autowired
    private UserService userService;
    
    public String login(String username, String password) {
        logger.info("尝试登录用户: {}", username);
        
        if (username == null || password == null) {
            throw new RuntimeException("用户名或密码不能为空");
        }
        
        if (userService.login(username, password)) {
            String token = jwtUtil.generateToken(username);
            logger.info("用户 {} 登录成功", username);
            return token;
        } else {
            logger.warn("用户 {} 登录失败: 用户名或密码错误", username);
            throw new RuntimeException("用户名或密码错误");
        }
    }
} 